//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ReplicatingVarianceSwapEngine.h"
using namespace Cephei::QL::Pricingengines::Forward;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::CReplicatingVarianceSwapEngine (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ dk, Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Double>^>^ callStrikes, Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Double>^>^ putStrikes) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cprocess;
    CoVector<Double>^ _CcallStrikes;
    CoVector<Double>^ _CputStrikes;
    try
    {
#ifdef HANDLE
        _phReplicatingVarianceSwapEngine = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Real _dk = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (dk) ? (QuantLib::Real)ValueHelper::Convert (dk->Value) : 5.0); //4
        CDoubleVector^ _NCcallStrikes;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Double>^>::IsSome::get (callStrikes))
        {
            _CcallStrikes = safe_cast<CoVector<Double>^> (callStrikes->Value);
            _CcallStrikes->Lock();
            INativeVector<Double>^ _NCIcallStrikes = _CcallStrikes->getFeature (NativeFeature::Value);
            _NCcallStrikes = safe_cast<CDoubleVector^>(_NCIcallStrikes);
        }
        std::vector<QuantLib::Real>& _callStrikes = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Double>^>::IsSome::get (callStrikes) ? static_cast<std::vector<QuantLib::Real>&> (_NCcallStrikes->GetReference ()) : std::vector<QuantLib::Real>()); //3
        CDoubleVector^ _NCputStrikes;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Double>^>::IsSome::get (putStrikes))
        {
            _CputStrikes = safe_cast<CoVector<Double>^> (putStrikes->Value);
            _CputStrikes->Lock();
            INativeVector<Double>^ _NCIputStrikes = _CputStrikes->getFeature (NativeFeature::Value);
            _NCputStrikes = safe_cast<CDoubleVector^>(_NCIputStrikes);
        }
        std::vector<QuantLib::Real>& _putStrikes = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Double>^>::IsSome::get (putStrikes) ? static_cast<std::vector<QuantLib::Real>&> (_NCputStrikes->GetReference ()) : std::vector<QuantLib::Real>()); //3
        _ppReplicatingVarianceSwapEngine = new boost::shared_ptr<QuantLib::ReplicatingVarianceSwapEngine> (new QuantLib::ReplicatingVarianceSwapEngine ( _process,  _dk,  _callStrikes,  _putStrikes ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
        if (_CcallStrikes != nullptr) _CcallStrikes->Unlock();
        if (_CputStrikes != nullptr) _CputStrikes->Unlock();
    }
}
Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::CReplicatingVarianceSwapEngine (boost::shared_ptr<QuantLib::ReplicatingVarianceSwapEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phReplicatingVarianceSwapEngine = NULL;
#endif
	_ppReplicatingVarianceSwapEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::CReplicatingVarianceSwapEngine (QuantLib::ReplicatingVarianceSwapEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phReplicatingVarianceSwapEngine = NULL;
#endif
	_ppReplicatingVarianceSwapEngine = new boost::shared_ptr<QuantLib::ReplicatingVarianceSwapEngine> (&childNative);
    
    _ReplicatingVarianceSwapEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::CReplicatingVarianceSwapEngine (CReplicatingVarianceSwapEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phReplicatingVarianceSwapEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppReplicatingVarianceSwapEngine = new boost::shared_ptr<QuantLib::ReplicatingVarianceSwapEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::CReplicatingVarianceSwapEngine (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phReplicatingVarianceSwapEngine = NULL;
#endif
	if (!t->IsSubclassOf(CReplicatingVarianceSwapEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::CReplicatingVarianceSwapEngine (QuantLib::Handle<QuantLib::ReplicatingVarianceSwapEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phReplicatingVarianceSwapEngine = &childNative;
	_ppReplicatingVarianceSwapEngine = &static_cast<boost::shared_ptr<QuantLib::ReplicatingVarianceSwapEngine>>(childNative.currentLink());
    
    _ReplicatingVarianceSwapEngineOwner = owner;
}
Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::CReplicatingVarianceSwapEngine (QuantLib::Handle<QuantLib::ReplicatingVarianceSwapEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phReplicatingVarianceSwapEngine = &childNative;
	_ppReplicatingVarianceSwapEngine = &static_cast<boost::shared_ptr<QuantLib::ReplicatingVarianceSwapEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::CReplicatingVarianceSwapEngine (QuantLib::ReplicatingVarianceSwapEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phReplicatingVarianceSwapEngine = NULL;
#endif
	_ppReplicatingVarianceSwapEngine = new boost::shared_ptr<QuantLib::ReplicatingVarianceSwapEngine> (new QuantLib::ReplicatingVarianceSwapEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::~CReplicatingVarianceSwapEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppReplicatingVarianceSwapEngine != NULL)
    {
	    delete _ppReplicatingVarianceSwapEngine;
        _ppReplicatingVarianceSwapEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::!CReplicatingVarianceSwapEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppReplicatingVarianceSwapEngine != NULL)
    {
	    delete _ppReplicatingVarianceSwapEngine;
    }
}
QuantLib::ReplicatingVarianceSwapEngine& Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::GetReference ()
{
    if (_ppReplicatingVarianceSwapEngine == NULL) throw gcnew NativeNullException ();
	return **_ppReplicatingVarianceSwapEngine;
}
boost::shared_ptr<QuantLib::ReplicatingVarianceSwapEngine>& Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::GetShared ()
{
    if (_ppReplicatingVarianceSwapEngine == NULL) throw gcnew NativeNullException ();
	return *_ppReplicatingVarianceSwapEngine;
}
QuantLib::ReplicatingVarianceSwapEngine* Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::GetPointer ()
{
    if (_ppReplicatingVarianceSwapEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppReplicatingVarianceSwapEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ReplicatingVarianceSwapEngine>& Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::GetHandle ()
{
	if (_phReplicatingVarianceSwapEngine == NULL)
	{
		_phReplicatingVarianceSwapEngine = new Handle<QuantLib::ReplicatingVarianceSwapEngine> (*_ppReplicatingVarianceSwapEngine);
	}
	return *_phReplicatingVarianceSwapEngine;
}
#endif
bool Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::HasNative () 
{
	return (_ppReplicatingVarianceSwapEngine != NULL);
}

Cephei::QL::Pricingengines::Forward::IReplicatingVarianceSwapEngine^ Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine::Calculate::get ()
{
    try
    {
    	(*_ppReplicatingVarianceSwapEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Forward::IReplicatingVarianceSwapEngine^ Cephei::QL::Pricingengines::Forward::CReplicatingVarianceSwapEngine_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ dk, Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Double>^>^ callStrikes, Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Double>^>^ putStrikes)
{
    return gcnew CReplicatingVarianceSwapEngine ( process,  dk,  callStrikes,  putStrikes);
}
